var PayServiceInfoDlg = {
    payServiceInfoData: {},
    zTreeInstance: null,
    validateFields: {
        department: {
            validators: {
                notEmpty: {
                    message: '科室名称不能为空'
                },
                stringLength: {
                    max: 20,
                    message: '科室名称长度不能超过20，请重新输入...'
                }/*最后一个没有逗号*/
            }
        },
        firstLevelProject: {
            validators: {
                notEmpty: {
                    message: '一级项目不能为空'
                },
                stringLength: {
                    max: 20,
                    message: '一级项目长度不能超过20，请重新输入...'
                }/*最后一个没有逗号*/
            }
        },
        payServiceName: {
            validators: {
                notEmpty: {
                    message: '产品项目名称不能为空'
                },
                stringLength: {
                    max: 70,
                    message: '产品项目名称长度不能超过70，请重新输入...'
                }/*最后一个没有逗号*/
            }
        },
        payServicePriceOne: {
            validators: {
                notEmpty: {
                    message: '产品价格1不能为空'
                },
            }
        },
        payServicePriceTwo: {
            validators: {
                notEmpty: {
                    message: '产品价格2不能为空'
                },
            }
        },
    }
};
//清除数据
PayServiceInfoDlg.clearData = function () {
    this.payServiceInfoData = {};
};
//设置对话框中的数据 @param key 数据的名称 @param val 数据的具体值
PayServiceInfoDlg.set = function (key, val) {
    this.payServiceInfoData[key] = (typeof value == "undefined") ? $("#" + key).val() : value;
    return this;
};
//设置对话框中的数据 @param key 数据的名称 @param val 数据的具体值
PayServiceInfoDlg.get = function (key) {
    return $("#" + key).val();
};
//关闭对话框
PayServiceInfoDlg.close = function () {
    parent.layer.close(parent.layer.getFrameIndex(window.name));
};
//点击部门ztree列表的选项时 @param e @param treeId @param treeNode @returns
PayServiceInfoDlg.onClickPayService = function (e, treeId, treeNode) {
    $("#pName").attr("value", PayServiceInfoDlg.zTreeInstance.getSelectedVal());
    $("#pid").attr("value", treeNode.id);
};
//显示部门选择的树 @returns
PayServiceInfoDlg.showPayServiceSelectTree = function () {
    var pName = $("#pName");
    var pNameOffset = $("#pName").offset();
    $("#parentPayServiceMenu").css({
        left: pNameOffset.left + "px",
        top: pNameOffset.top + pName.outerHeight() + "px"
    }).slideDown("fast");

    $("body").bind("mousedown", onBodyDown);
};
//隐藏部门选择的树
PayServiceInfoDlg.hidePayServiceSelectTree = function () {
    $("#parentPayServiceMenu").fadeOut("fast");
    $("body").unbind("mousedown", onBodyDown);// mousedown当鼠标按下就可以触发，不用弹起
};
//收集数据
PayServiceInfoDlg.collectData = function () {
    this.set('id').set('payService').set('updateMoney').set('totalAmount').set('referraler').set('rid')
        .set('payServiceNum').set('department').set('firstLevelProject').set('payServiceId').set('isTeethProject')
        .set('unit').set('remarks').set('customerId').set('zheMoney').set('num').set('payServiceName').set('payServicePriceOne')
        .set('payServicePriceTwo').set('leftUp').set('rightUp').set('leftDown').set('rightDown').set('doctor').set('nurse').set('referraler');
};
//验证数据是否为空
PayServiceInfoDlg.validate = function () {
    $('#payServiceInfoForm').data("bootstrapValidator").resetForm();
    $('#payServiceInfoForm').bootstrapValidator('validate');
    return $("#payServiceInfoForm").data('bootstrapValidator').isValid();
};
//提交添加
PayServiceInfoDlg.addSubmit = function () {
    this.clearData();
    this.collectData();
    var payServicePriceOne = $('#payServicePriceOne').val();
    var payServicePriceTwo = $('#payServicePriceTwo').val();
    if (payServicePriceOne != "" && payServicePriceOne != null) {
        if (!(/^[0-9]{1}\d{0,6}(\.\d{1,2})?$/.test(payServicePriceOne))) {
            alert("产品价格1整数位不超过7位，小数点后不超过2位小数，请重新输入...！");
            return;
        }
    }
    if (payServicePriceTwo != "" && payServicePriceTwo != null) {
        if (!(/^[0-9]{1}\d{0,6}(\.\d{1,2})?$/.test(payServicePriceTwo))) {
            alert("产品价格2整数位不超过7位，小数点后不超过2位小数，请重新输入...！");
            return;
        }
    }
    var ajax = new $ax(Feng.ctxPath + "/payService/addPayService", function (data) {
        Feng.success("添加成功!");
        window.parent.PayService.table.refresh();
        PayServiceInfoDlg.close();
    }, function (data) {
        Feng.error("添加失败!" + data.responseJSON.message + "!");
    });
    ajax.set(this.payServiceInfoData);
    ajax.start();
};
//提交修改
var getCount = 0;
PayServiceInfoDlg.editSubmit = function () {
    this.clearData();
    this.collectData();
    var payServicePriceOne = $('#payServicePriceOne').val();
    var payServicePriceTwo = $('#payServicePriceTwo').val();
    if (payServicePriceOne != "" && payServicePriceOne != null) {
        if (!(/^[0-9]{1}\d{0,6}(\.\d{1,2})?$/.test(payServicePriceOne))) {
            alert("产品价格1整数位不超过7位，小数点后不超过2位小数，请重新输入...！");
            return;
        }
    }
    if (payServicePriceTwo != "" && payServicePriceTwo != null) {
        if (!(/^[0-9]{1}\d{0,6}(\.\d{1,2})?$/.test(payServicePriceTwo))) {
            alert("产品价格2整数位不超过7位，小数点后不超过2位小数，请重新输入...！");
            return;
        }
    }
    var ajax = new $ax(Feng.ctxPath + "/payService/updatePayService", function (data) {//提交信息
        Feng.success("修改成功!");
        window.parent.PayService.table.refresh();
        PayServiceInfoDlg.close();
    }, function (data) {
        Feng.error("修改失败!" + data.responseJSON.message + "!");
    });
    ajax.set(this.payServiceInfoData);
    ajax.start();
};

function onBodyDown(event) {
    if (!(event.target.id == "menuBtn" || event.target.id == "parentPayServiceMenu" || $(
        event.target).parents("#parentPayServiceMenu").length > 0)) {
        PayServiceInfoDlg.hidePayServiceSelectTree();
    }
};

var tips;
$("#inputBox").click(function (e) {
    e.stopPropagation();
    tips = layer.open({
        type: 4,
        tips: [1, '#ffffff'],
        closeBtn: 0,
        area: ['auto', 'auto'],
        content: ['<div>' + $("#noneBox").html() + '</div>', '#inputBox'],
    });
});

$(document).click(function () {
    layer.close(tips)
});

$(document).on('click', '.tipsUl>li', function (e) {
    e.stopPropagation();
    $(this).addClass('tipsLi').siblings().removeClass('tipsLi');
    if ($(this).index() == 0) {
        $(this).parent().siblings().children('#permanent').show().siblings().hide()
    } else if ($(this).index() == 1) {
        $(this).parent().siblings().children('#deciduous').show().siblings().hide()
    }
});

$(document).on('click', '.toothTopLeft>span', function (e) {
    e.stopPropagation();
    var text = $(this).text();
    if ($(this).children().attr('class') == 'spanStyle') {
        var inputValue = $("#leftUp").val().replace(text, "");
        $("#leftUp").val(inputValue);
        $(this).children().removeClass('spanStyle');
    } else {
        $(this).children().addClass('spanStyle');
        $("#leftUp").val($("#leftUp").val() + text)
    }
});

$(document).on('click', '.toothTopRight>span', function (e) {
    e.stopPropagation();
    var text = $(this).text();
    if ($(this).children().attr('class') == 'spanStyle') {
        var inputValue = $("#rightUp").val().replace(text, "");
        $("#rightUp").val(inputValue);
        $(this).children().removeClass('spanStyle');
    } else {
        $(this).children().addClass('spanStyle');
        $("#rightUp").val($("#rightUp").val() + text)
    }
});

$(document).on('click', '.toothBottomLeft>span', function (e) {
    e.stopPropagation();
    var text = $(this).text();
    if ($(this).children().attr('class') == 'spanStyle') {
        var inputValue = $("#leftDown").val().replace(text, "");
        $("#leftDown").val(inputValue);
        $(this).children().removeClass('spanStyle');
    } else {
        $(this).children().addClass('spanStyle');
        $("#leftDown").val($("#leftDown").val() + text)
    }
});

$(document).on('click', '.toothBottomRight>span', function (e) {
    e.stopPropagation();
    var text = $(this).text();
    if ($(this).children().attr('class') == 'spanStyle') {
        var inputValue = $("#rightDown").val().replace(text, "");
        $("#rightDown").val(inputValue);
        $(this).children().removeClass('spanStyle');
    } else {
        $(this).children().addClass('spanStyle');
        $("#rightDown").val($("#rightDown").val() + text)
    }
});

function getPayServiceOne() {
    $.ajax({
        url: Feng.ctxPath + "/payService/getPayServiceOne",
        type: "post",
        data: {
            "department": $("#department").val()
        },
        success: function (data) {
            var txt = '<option value="">请选择</option>';
            for (var i = 0; i < data.length; i++) {
                txt += '<option value="' + data[i].firstLevelProject + '">' + data[i].firstLevelProject + '</option>';
            }
            document.getElementById('firstLevelProject').innerHTML = txt;
            console.log($("#firstLevelProjectValue").val());
            if ($("#firstLevelProjectValue").val() != "") {
                $("#firstLevelProject option[value='" + $("#firstLevelProjectValue").val() + "']").attr("selected", "selected");
            }
        }
    });
};

$(function () {
    if ($("#departmentValue").val() != "") {
        $("#department option[value='" + $("#departmentValue").val() + "']").attr("selected", "selected");
        getPayServiceOne()
    }
});